package com.ruoyi.activiti.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.ruoyi.activiti.domain.ActMaintainOrder;
import com.ruoyi.activiti.domain.vo.ActMaintainOrderVO;
import com.ruoyi.activiti.domain.vo.FieldVO;
import com.ruoyi.activiti.mapper.ActMaintainOrderMapper;
import com.ruoyi.activiti.service.IActMaintainOrderService;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 维护单Service业务层处理
 *
 * @author cjie
 * @date 2021-12-19
 */
@Service
public class ActMaintainOrderServiceImpl implements IActMaintainOrderService {
    @Autowired
    private ActMaintainOrderMapper actMaintainOrderMapper;

    /**
     * 查询维护单
     *
     * @param id 维护单ID
     * @return 维护单
     */
    @Override
    public ActMaintainOrder selectActMaintainOrderById(String id) {
        return actMaintainOrderMapper.selectActMaintainOrderById(id);
    }

    /**
     * 查询维护单列表
     *
     * @param actMaintainOrder 维护单
     * @return 维护单
     */
    @Override
    public List<ActMaintainOrder> selectActMaintainOrderList(ActMaintainOrder actMaintainOrder) {
        return actMaintainOrderMapper.selectActMaintainOrderList(actMaintainOrder);
    }

    /**
     * 新增维护单
     *
     * @param actMaintainOrder 维护单
     * @return 结果
     */
    @Override
    public int insertActMaintainOrder(ActMaintainOrder actMaintainOrder) {
        actMaintainOrder.setId(IdUtil.getSnowflake(0, 0).nextIdStr());
        actMaintainOrder.setCreateTime(DateUtils.getNowDate());
        actMaintainOrder.setCreateBy(SecurityUtils.getUsername());
        return actMaintainOrderMapper.insertActMaintainOrder(actMaintainOrder);
    }

    /**
     * 修改维护单
     *
     * @param actMaintainOrder 维护单
     * @return 结果
     */
    @Override
    public int updateActMaintainOrder(ActMaintainOrder actMaintainOrder) {
        actMaintainOrder.setUpdateTime(DateUtils.getNowDate());
        actMaintainOrder.setUpdateBy(SecurityUtils.getUsername());
        return actMaintainOrderMapper.updateActMaintainOrder(actMaintainOrder);
    }

    /**
     * 批量删除维护单
     *
     * @param ids 需要删除的维护单ID
     * @return 结果
     */
    @Override
    public int deleteActMaintainOrderByIds(String[] ids) {
        return actMaintainOrderMapper.deleteActMaintainOrderByIds(ids);
    }

    /**
     * 删除维护单信息
     *
     * @param id 维护单ID
     * @return 结果
     */
    @Override
    public int deleteActMaintainOrderById(String id) {
        return actMaintainOrderMapper.deleteActMaintainOrderById(id);
    }

    @Override
    public int insertActMaintainOrderVO(ActMaintainOrderVO actMaintainOrderVO) {
        //this.checkFields(actMaintainOrderVO.getFields());
        // 插入
        ActMaintainOrder actMaintainOrder = new ActMaintainOrder();
        BeanUtils.copyProperties(actMaintainOrderVO,actMaintainOrder);
        //ActMaintainOrder actMaintainOrder = ActMaintainOrderConvert.INSTANCE.convert(actMaintainOrderVO);

        actMaintainOrder.setFields(covertList2One(actMaintainOrderVO.getFields()));
//        BpmFormDO form = BpmFormConvert.INSTANCE.convert(createReqVO);
//        formMapper.insert(form);
//        // 返回
//        return form.getId();
        return this.insertActMaintainOrder(actMaintainOrder);
    }

    @Override
    public ActMaintainOrderVO selectActMaintainOrderVOById(String id) {
        ActMaintainOrder actMaintainOrder = this.selectActMaintainOrderById(id);
        ActMaintainOrderVO actMaintainOrderVO = new ActMaintainOrderVO();
        BeanUtils.copyProperties(actMaintainOrder,actMaintainOrderVO);
        actMaintainOrderVO.setFields(this.convertOne2List(actMaintainOrder.getFields()));
        return actMaintainOrderVO;
    }

    @Override
    public int updateActMaintainOrderVO(ActMaintainOrderVO actMaintainOrderVO) {
        ActMaintainOrder actMaintainOrder = new ActMaintainOrder();
        BeanUtils.copyProperties(actMaintainOrderVO,actMaintainOrder);

        actMaintainOrder.setFields(covertList2One(actMaintainOrderVO.getFields()));
        return this.updateActMaintainOrder(actMaintainOrder);
    }

    @Override
    public List<FieldVO> selectFieldVOByFormId(String id) {
        ActMaintainOrder actMaintainOrder = this.selectActMaintainOrderById(id);
        List<FieldVO> result = new ArrayList<>();
        JSONArray jsonArray = new JSONArray(actMaintainOrder.getFields());
        for(int j =0;j<jsonArray.size();j++){
            JSONObject object = jsonArray.getJSONObject(j);
            String prop = object.getStr("__vModel__");
            JSONObject label = object.getJSONObject("__config__");
            FieldVO fieldVO = new FieldVO();
            fieldVO.setLabel(label.getStr("label"));
            fieldVO.setProp(prop);
            result.add(fieldVO);

        }
        return result;
    }

    private String covertList2One(List<String> arr){
        return JSONUtil.toJsonStr(arr);
    }

    private List<String> convertOne2List(String one){
        //return Arrays.asList(one.split(";")).stream().map(s->(s.trim())).collect(Collectors.toList());
        return JSONUtil.toList(JSONUtil.parseArray(one), String.class);
    }
    /**
     * 校验 Field，避免 field 重复
     *
     * @param fields field 数组
     */
//    private void checkFields(List<String> fields) {
//        Map<String, String> fieldMap = new HashMap<>(); // key 是 vModel，value 是 label
//        for (String field : fields) {
//            BpmFormFieldRespDTO fieldDTO = JsonUtils.parseObject(field, BpmFormFieldRespDTO.class);
//            Assert.notNull(fieldDTO);
//            String oldLabel = fieldMap.put(fieldDTO.getVModel(), fieldDTO.getLabel());
//            // 如果不存在，则直接返回
//            if (oldLabel == null) {
//                continue;
//            }else {
//                //throw new CustomException();
//            }
//
//            // 如果存在，则报错
//            //throw ServiceExceptionUtil.exception(ErrorCodeConstants.FORM_FIELD_REPEAT, oldLabel, fieldDTO.getLabel(), fieldDTO.getVModel());
//        }
//    }


}
